var $form;
var form;
var $;
layui.config({
    base: "../../js/"
}).use(['form', 'layer', 'upload', 'laydate'], function () {
    form = layui.form();
    var layer = parent.layer === undefined ? layui.layer : parent.layer;
    $ = layui.jquery;
    $form = $('form');
    laydate = layui.laydate;


    var filedir = "cmsRecommend/"; // 对应oss目录

    var infoName = [];
    var infoIds = [];

    var rootDom = window.parent.document.getElementById("rootPath");
    var root = '/';
    if(rootDom) root = rootDom.value;
    $("#infoSel").click(function(){
        var index = layui.layer.open({
            title : "资讯选择",
            type : 2,
            content : root+"/information/informationOpenListPage",
            shadeClose: true,
            closeBtn: 1,
            maxmin: true, //开启最大化最小化按钮
            area: ['860px', '560px'],
            btn: ['确认'],
            yes: function(index, layero){
                var $checked = $("#informations_list tbody input[type=\"radio\"][name=\"checked\"]:checked", layero.find("iframe")[0].contentWindow.document);
                var $tr = $checked.closest("tr");
                var infoId = $tr.find("td:eq(1)").attr("infoId");
                var infoName = $tr.find("td:eq(1)").text();

                $("#fromId").val(infoId);
                $("#infoSel").val(infoName);


                layui.layer.closeAll();
            }
            // success : function(layer, index){
            //     setTimeout(function(){
            //         layui.layer.tips('点击此处返回剧集列表', '.layui-layer-setwin .layui-layer-close', {
            //             tips: 3
            //         });
            //     },500)
            // }
        })
    });



    $('#resetBtn').click(function(){
        window.location.reload();
    });

    /* upload({
         url: "../../json/userface.json",
         success: function (res) {
             var num = parseInt(4 * Math.random());  //生成0-4的随机数
             //随机显示一个头像信息
             userFace.src = res.data[num].src;
             window.sessionStorage.setItem('userFace', res.data[num].src);
         }
     });

     //判断是否修改过头像，如果修改过则显示修改后的头像，否则显示默认头像
    if (window.sessionStorage.getItem('userFace')) {
         $("#userFace").attr("src", window.sessionStorage.getItem('userFace'));
     } else {
         $("#userFace").attr("src", "../../images/face.jpg");
     }*/
    layui.upload({
        url: '/oss/ossImgUpload?filedir='+filedir,
        before: function(input){
            //返回的参数item，即为当前的input DOM对象
            console.log('图片上传中');
        },
        success: function(res){
            console.log('图片上传完毕');
            console.log(">>>>>>>>>>>>>>>"+JSON.stringify(res));
            $("#cmsRecommendImg").attr("src",res.link);
            $("#imgUrl").val(res.link);
        }
    });
    //提交个人资料
    form.on("submit(submitCmsRecommend)", function (data) {
        // var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //将填写的用户信息存到session以便下次调取
        var data = $("#cmsRecommendAddForm").serialize();

        $.ajax({
            cache: false,
            type: 'POST',
            url: "/cmsRecommend/addCmsRecommend",
            dataType: "json",
            data: data,
            async: false,
            success: function (data) {
                if (data.result === "success") {
                    top.layer.alert("保存成功", {icon: 1});
                } else {
                    top.layer.alert("保存异常,请重试", {icon: 0});
                }
            },
            error: function () {
                top.layer.alert("发生异常,请重试", {icon: 2});
            }
        })
        return false;
    })


})
